package br.uneb.tebd.model;

import java.util.ArrayList;
import java.util.List;

import br.uneb.tebd.util.DatabaseInstance;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class GuitarRepository {
	
	private SQLiteDatabase database;
	public static final String TABLE_NAME = "guitars";
	public static final String FIELDS[] = { GuitarFields.ID.getLiteral(), 
											GuitarFields.DESCRIPTION.getLiteral(),
											GuitarFields.MODEL.getLiteral(), 
											GuitarFields.BRAND.getLiteral() };
	
	public GuitarRepository(Context context) {
		this.database = DatabaseInstance.GET.getDatabaseInstance(context);
		this.database.execSQL("create table if not exists " + TABLE_NAME +" (id integer primary key autoincrement," +
		"description text not null, model text not null, brand text not null);");
	}
	
	public long save(Guitar guitar) {
		
		ContentValues values = new ContentValues();
		
		values.put(GuitarFields.DESCRIPTION.getLiteral(), guitar.getDescription());
		values.put(GuitarFields.MODEL.getLiteral(), guitar.getModel());
		values.put(GuitarFields.BRAND.getLiteral(), guitar.getBrand());
		
		return this.database.insert(TABLE_NAME, null, values);
	}
	
	public int update(Guitar guitar) {
		ContentValues values = new ContentValues();
		values.put(GuitarFields.DESCRIPTION.getLiteral(), guitar.getDescription());
		values.put(GuitarFields.MODEL.getLiteral(), guitar.getModel());
		values.put(GuitarFields.BRAND.getLiteral(), guitar.getBrand());

		return this.database.update(TABLE_NAME, values, "id=" + guitar.getId(), null);
	}
	
	private Cursor getCursor() {
		
		try {
			
			return database.query(TABLE_NAME, FIELDS, 
					null, null, null, null, null);
			
		} catch (SQLException e) {
			Log.e("ERRO", "Erro ao buscar as guitarras " + e.toString());
			return null;
		}
	}
	
	public List<Guitar> listGuitars() {
		
		Cursor cursor = this.getCursor();
		
		List<Guitar> guitars = new ArrayList<Guitar>();
		
		if (cursor.moveToFirst()) {
			
			int indexId = cursor.getColumnIndex(GuitarFields.ID.getLiteral());
			int indexDescription = cursor.getColumnIndex(GuitarFields.DESCRIPTION.getLiteral());
			int indexModel = cursor.getColumnIndex(GuitarFields.MODEL.getLiteral());
			int indexBrand = cursor.getColumnIndex(GuitarFields.BRAND.getLiteral());
			
			do {
				
				Guitar guitar = new Guitar();
				guitar.setId(cursor.getLong(indexId));
				guitar.setDescription(cursor.getString(indexDescription));
				guitar.setModel(cursor.getString(indexModel));
				guitar.setBrand(cursor.getString(indexBrand));
				
				guitars.add(guitar);
				
				
			} while (cursor.moveToNext());
			
			
		}
		
		return guitars;
		
	}
	
	public void closeDatabase() {
		
		if (database != null) {
			this.database.close();
		}
		
	}
	
	public int deleteGuitar(long id) {
		
		String where = GuitarFields.ID.getLiteral() + "=?";
		String[] whereArgs = new String[] { String.valueOf(id) };
		
		int count = delete(where, whereArgs);
		
		return count;
		
	}
	
	public int deleteGuitar(String description) {
		
		String where = GuitarFields.DESCRIPTION.getLiteral() + " LIKE ?";
		String[] whereArgs = new String[] { description };
		
		int count = delete(where, whereArgs);
		
		return count;
		
	}
	
	private int delete(String where, String[] whereArgs) {
		
		int count = database.delete(TABLE_NAME, where, whereArgs);
		
		return count;
	}
	
//	public Guitar findGuitar(long id, String description){
//		String idText = "";
//		String descriptionText = "";
//		if(id > 0){
//			idText = GuitarFields.ID.getLiteral() + "=" + id;
//		}
//		if(!descriptionText.equalsIgnoreCase("")){
//			if(id > 0){
//				descriptionText += " AND ";
//			}
//			descriptionText = GuitarFields.DESCRIPTION.getLiteral() + " LIKE '" + description + "'";
//		}
//		
//		Cursor cursor = database.query(true, TABLE_NAME, FIELDS, idText + descriptionText, null, null, null, null, null);
//		if (cursor.getCount() > 0) {
//
//			cursor.moveToFirst();
//
//			Guitar guitar = new Guitar();
//			guitar.setId(cursor.getLong(0));
//			guitar.setDescription(cursor.getString(1));
//			guitar.setModel(cursor.getString(2));
//			guitar.setBrand(cursor.getString(3));
//
//			return guitar;
//		}
//		return null;
//	}
	
	public Guitar findGuitarById(long id) {
		Cursor cursor = database.query(true, TABLE_NAME, FIELDS, GuitarFields.ID.getLiteral() + "=" + id, null, null, null, null, null);
		
		if (cursor.getCount() > 0) {
			
			cursor.moveToFirst();
			
			Guitar guitar = new Guitar();
			guitar.setId(cursor.getLong(0));
			guitar.setDescription(cursor.getString(1));
			guitar.setModel(cursor.getString(2));
			guitar.setBrand(cursor.getString(3));
			
			return guitar;
		}
		
		return null;
	}
	
	public Guitar findGuitarByDescription(String description) {
		Cursor cursor = database.rawQuery("select * from guitars where description like '%" + description + "%'", null);
//		Cursor cursor = database.query(true, TABLE_NAME, FIELDS, GuitarFields.DESCRIPTION.getLiteral(), new String[] { GuitarFields.DESCRIPTION.getLiteral() + "like " + "'%" + description + "%'" }, null, null, null, null);
		
		if (cursor.getCount() > 0) {

			cursor.moveToFirst();
			
			Guitar guitar = new Guitar();
			guitar.setId(cursor.getLong(0));
			guitar.setDescription(cursor.getString(1));
			guitar.setModel(cursor.getString(2));
			guitar.setBrand(cursor.getString(3));
			
			return guitar;
			
		}
		
		return null;
	}
}
